﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections;
using ProgNetComponents.NamedObjects;
using ProgNetComponents.Data.Query;

namespace ProgNetComponents.Design.TypeConverters
{
    public class CollectionTypeConverter : ExpandableObjectConverter
    {
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            PropertyDescriptorCollection coll = new PropertyDescriptorCollection(null);
            foreach (object o in value as CollectionBase)
            {
                coll.Add(new CollectionItemPropertyDescriptor(o, value as CollectionBase));
            }
            return coll;
        }

    }
    public class CollectionItemPropertyDescriptor : PropertyDescriptor
        {
            private CollectionBase collection;
            private object item;

            public CollectionItemPropertyDescriptor(object item, CollectionBase collection)
                : base(item.AsString(), null)
            {
                this.collection = collection;
                this.item = item;
            }

            public override bool CanResetValue(object component)
            {
                return true;
            }
            public override string DisplayName
            {
                get
                {
                    return item.AsString();
                }
            }
            public override Type ComponentType
            {
                get { return collection.GetType(); }
            }

            public override object GetValue(object component)
            {
                return item;
            }

            public override bool IsReadOnly
            {
                get { return false; }
            }

            public override Type PropertyType
            {
                get { return item.GetType(); }
            }

            public override void ResetValue(object component)
            {
                
            }

            public override void SetValue(object component, object value)
            {
                
            }

            public override bool ShouldSerializeValue(object component)
            {
                return true;
            }
        }
}
